<!DOCTYPE html>
<html lang="zh-CN">
  <head>
  <meta charset="UTF-8">
  <meta 
    name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0">
  <meta 
    http-equiv="X-UA-Compatible" 
    content="ie=edge">
  <meta 
    name="theme-color" 
    content="#fff" 
    id="theme-color">
  <meta 
    name="description" 
    content="hexoDemo用于介绍如何使用hexo">
  <link 
    rel="icon" 
    href="/">
  <title>15-内置对象 String：字符串的常见方法</title>
  
    
      <meta 
        property="og:title" 
        content="15-内置对象 String：字符串的常见方法">
    
    
      <meta 
        property="og:url" 
        content="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/15-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%20String%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%B8%B8%E8%A7%81%E6%96%B9%E6%B3%95/index.html">
    
    
      <meta 
        property="og:img" 
        content="https://img.songhn.com/img/Y67gdd.png">
    
    
      <meta 
        property="og:img" 
        content="hexoDemo用于介绍如何使用hexo">
    
    
      <meta 
        property="og:type" 
        content="article">
      <meta 
        property="og:article:published_time" 
        content="2004-03-01">
      <meta 
        property="og:article:modified_time" 
        content="2023-03-14">
      <meta 
        property="og:article:author" 
        content="zwl5670">
      
        
          <meta 
            property="og:article:tag" 
            content="JS基础">
        
      
    
  
  
  <link rel="preload" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css" as="style" >
  <link rel="preload" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css" as="style" >
  <link rel="preload" href="/css/main.css" as="style" >
  
  <link rel="modulepreload" href="//instant.page/5.1.0">
  
  <link rel="stylesheet" href="/css/main.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1946621_i1kgafibvw.css">
  
  <link rel="stylesheet" href="//at.alicdn.com/t/font_1952792_89b4ac4k4up.css">
  
  
  
    <link rel="stylesheet" href="/js/lib/lightbox/baguetteBox.min.css">
  
  <script>
    function loadScript(url, cb) {
      var script = document.createElement('script');
      script.src = url;
      if (cb) script.onload = cb;
      script.async = true;
      document.body.appendChild(script);
    }
    function loadCSS(href, data, attr) {
      var sheet = document.createElement('link');
      sheet.ref = 'stylesheet';
      sheet.href = href;
      sheet.dataset[data] = attr;
      document.head.appendChild(sheet);
    }
    function changeCSS(cssFile, data, attr) {
      var oldlink = document.querySelector(data);
      var newlink = document.createElement("link");
      newlink.setAttribute("rel", "stylesheet");
      newlink.setAttribute("href", cssFile);
      newlink.dataset.prism = attr;
      document.head.replaceChild(newlink, oldlink);
    }
  </script>
  
    
  
  <script>
    // control reverse button
    var reverseDarkList = {
      dark: 'light',
      light: 'dark'
    };
    var themeColor = {
      dark: '#1c1c1e',
      light: '#fff'
    }
    // get the data of css prefers-color-scheme
    var getCssMediaQuery = function() {
      return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
    };
    // reverse current darkmode setting function
    var reverseDarkModeSetting = function() {
      var setting = localStorage.getItem('user-color-scheme');
      if(reverseDarkList[setting]) {
        setting = reverseDarkList[setting];
      } else if(setting === null) {
        setting = reverseDarkList[getCssMediaQuery()];
      } else {
        return;
      }
      localStorage.setItem('user-color-scheme', setting);
      return setting;
    };
    // apply current darkmode setting
  </script>
  
    <script>
      var setDarkmode = function(mode) {
      var setting = mode || localStorage.getItem('user-color-scheme');
      if(setting === getCssMediaQuery()) {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else if(reverseDarkList[setting]) {
        document.documentElement.setAttribute('data-user-color-scheme', setting);
        document.getElementById('theme-color').content = themeColor[setting];
        document.getElementById('theme-color').dataset.mode = setting;
      } else {
        document.documentElement.removeAttribute('data-user-color-scheme');
        localStorage.removeItem('user-color-scheme');
        document.getElementById('theme-color').content = themeColor[getCssMediaQuery()];
        document.getElementById('theme-color').dataset.mode = getCssMediaQuery();
      }
    };
    setDarkmode();
    </script>
  
  
  
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.js" as="script">
    <link rel="preload" href="/js/lib/lightbox/baguetteBox.min.css" as="style" >
  
  
    <link rel="preload" href="/js/lib/lozad.min.js" as="script">
  
  
  
  
  
  
<meta name="generator" content="Hexo 6.3.0"></head>

  <body>
    <div class="wrapper">
       
      <nav class="navbar">
  <div class="navbar-logo">
    <a class="navbar-logo-main" href="/">
      
      <span class="navbar-logo-dsc">前端面试</span>
      </a>
  </div>
  <div class="navbar-menu">
    
      <a 
        href="/" 
        class="navbar-menu-item">
        
          首页
        
      </a>
    
      <a 
        href="/archives" 
        class="navbar-menu-item">
        
          归档
        
      </a>
    
      <a 
        href="/tags" 
        class="navbar-menu-item">
        
          标签
        
      </a>
    
      <a 
        href="/categories" 
        class="navbar-menu-item">
        
          分类
        
      </a>
    
      <a 
        href="/about" 
        class="navbar-menu-item">
        
          关于
        
      </a>
    
      <a 
        href="/links" 
        class="navbar-menu-item">
        
          友链
        
      </a>
    
    <button 
      class="navbar-menu-item darknavbar navbar-menu-btn" 
      aria-label="Toggle dark mode"
      id="dark">
      <i class="iconfont icon-weather"></i>
    </button>
    <button 
      class="navbar-menu-item searchnavbar navbar-menu-btn" 
      aria-label="Toggle search"
      id="search">
      <!-- <i 
        class="iconfont icon-search" 
        style="font-size: 1.2rem; font-weight: 400;">
      </i> -->
      <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img"
        class="iconify iconify--ion" width="28" height="28" preserveAspectRatio="xMidYMid meet" viewBox="0 0 512 512">
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M256 80a176 176 0 1 0 176 176A176 176 0 0 0 256 80Z"></path>
        <path fill="none" stroke="currentColor" stroke-miterlimit="10" stroke-width="28"
          d="M232 160a72 72 0 1 0 72 72a72 72 0 0 0-72-72Z"></path>
        <path fill="none" stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="28"
          d="M283.64 283.64L336 336"></path>
      </svg>
    </button>
  </div>
</nav> 
      
      <div 
        id="local-search" 
        style="display: none">
        <input
          class="navbar-menu-item"
          id="search-input"
          placeholder="请输入搜索内容..." />
        <div id="search-content"></div>
      </div>
      
      <div class="section-wrap">
        <div class="container">
          <div class="columns">
            <aside class="left-column">
              
              <div class="card card-author">
                
  <img 
    src="https://img.songhn.com/img/Y67gdd.png" 
    class="author-img"
    width="88"
    height="88"
    alt="author avatar">

<p class="author-name">zwl5670</p>
<p class="author-description">前端学习资料</p>
<div class="author-message">
  <a 
    class="author-posts-count" 
    href="/archives">
    <span>215</span>
    <span>文章</span>
  </a>
  <a 
    class="author-categories-count" 
    href="/categories">
    <span>14</span>
    <span>分类</span>
  </a>
  <a 
    class="author-tags-count" 
    href="/tags">
    <span>14</span>
    <span>标签</span>
  </a>
</div>

              </div>
               <div class="sticky-tablet">
  
  
    <article class="display-when-two-columns spacer">
      <div class="card card-content toc-card">
        <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%E7%AE%80%E4%BB%8B"><span class="toc-text">内置对象简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%89%8D%E8%A8%80"><span class="toc-text">字符串前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9F%A5%E6%89%BE%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">查找字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81indexOf-x2F-lastIndexOf-%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%8C%87%E5%AE%9A%E5%86%85%E5%AE%B9%E7%9A%84%E7%B4%A2%E5%BC%95"><span class="toc-text">1、indexOf()&#x2F;lastIndexOf()：获取字符串中指定内容的索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#indexOf-%E4%B8%BE%E4%BE%8B"><span class="toc-text">indexOf 举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81search-%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%8C%87%E5%AE%9A%E5%86%85%E5%AE%B9%E7%9A%84%E7%B4%A2%E5%BC%95%EF%BC%88%E5%8F%82%E6%95%B0%E9%87%8C%E4%B8%80%E8%88%AC%E6%98%AF%E6%AD%A3%E5%88%99%EF%BC%89"><span class="toc-text">2、search()：获取字符串中指定内容的索引（参数里一般是正则）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81includes-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9"><span class="toc-text">3、includes()：字符串中是否包含指定的内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81startsWith-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E4%BB%A5%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9%E5%BC%80%E5%A4%B4"><span class="toc-text">4、startsWith()：字符串是否以指定的内容开头</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5%E3%80%81endsWith-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E4%BB%A5%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9%E7%BB%93%E5%B0%BE"><span class="toc-text">5、endsWith()：字符串是否以指定的内容结尾</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E4%BD%8D%E7%BD%AE%E7%9A%84%E5%AD%97%E7%AC%A6"><span class="toc-text">获取指定位置的字符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81charAt-index"><span class="toc-text">1、charAt(index)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81str-index"><span class="toc-text">2、str[index]</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81charCodeAt-index"><span class="toc-text">3、charCodeAt(index)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%88%AA%E5%8F%96"><span class="toc-text">字符串截取</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81slice"><span class="toc-text">1、slice()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81substring"><span class="toc-text">2、substring()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81substr"><span class="toc-text">3、substr()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#String-fromCharCode"><span class="toc-text">String.fromCharCode()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#concat"><span class="toc-text">concat()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#split-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B0%E7%BB%84-%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">split()：字符串转换为数组 【重要】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#replace"><span class="toc-text">replace()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#repeat-%EF%BC%9A%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">repeat()：重复字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#trim"><span class="toc-text">trim()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%A7%E5%B0%8F%E5%86%99%E8%BD%AC%E6%8D%A2"><span class="toc-text">大小写转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#html-%E6%96%B9%E6%B3%95"><span class="toc-text">html 方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BB%83%E4%B9%A0"><span class="toc-text">字符串练习</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
      </div>
    </article>
  
  
  <article class="card card-content categories-widget">
    <div class="categories-card">
  <div class="categories-header">
    <i 
      class="iconfont icon-fenlei" 
      style="padding-right: 2px;">
    </i>分类
  </div>
  <div class="categories-list">
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/">
        <div class="categories-list-item">
          前端工具
          <span class="categories-list-item-badge">13</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          CSS基础
          <span class="categories-list-item-badge">18</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          CSS进阶
          <span class="categories-list-item-badge">9</span>
        </div>
      </a>
    
      <a href="/categories/HTML/">
        <div class="categories-list-item">
          HTML
          <span class="categories-list-item-badge">12</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          JS基础
          <span class="categories-list-item-badge">56</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80ES6/">
        <div class="categories-list-item">
          JS基础ES6
          <span class="categories-list-item-badge">10</span>
        </div>
      </a>
    
      <a href="/categories/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/">
        <div class="categories-list-item">
          JS基础异步
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/JS%E8%BF%9B%E9%98%B6/">
        <div class="categories-list-item">
          JS进阶
          <span class="categories-list-item-badge">11</span>
        </div>
      </a>
    
      <a href="/categories/%E7%A7%BB%E5%8A%A8web/">
        <div class="categories-list-item">
          移动web
          <span class="categories-list-item-badge">3</span>
        </div>
      </a>
    
      <a href="/categories/CSS%E5%92%8CDOM/">
        <div class="categories-list-item">
          CSS和DOM
          <span class="categories-list-item-badge">6</span>
        </div>
      </a>
    
      <a href="/categories/Node/">
        <div class="categories-list-item">
          Node
          <span class="categories-list-item-badge">16</span>
        </div>
      </a>
    
      <a href="/categories/Vue%E5%9F%BA%E7%A1%80/">
        <div class="categories-list-item">
          Vue基础
          <span class="categories-list-item-badge">15</span>
        </div>
      </a>
    
      <a href="/categories/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/">
        <div class="categories-list-item">
          性能优化
          <span class="categories-list-item-badge">7</span>
        </div>
      </a>
    
      <a href="/categories/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/">
        <div class="categories-list-item">
          前端工程化
          <span class="categories-list-item-badge">8</span>
        </div>
      </a>
    
  </div>
</div>
  </article>
  
  <article class="card card-content tags-widget">
    <div class="tags-card">
  <div class="tags-header">
    <i 
      class="iconfont icon-biaoqian" 
      style="padding-right: 2px;">
    </i>热门标签
  </div>
  <div class="tags-list">
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80/" 
        title="JS基础">
        <div class="tags-list-item">JS基础</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%9F%BA%E7%A1%80/" 
        title="CSS基础">
        <div class="tags-list-item">CSS基础</div>
      </a>
    
      <a 
        href="/tags/Node/" 
        title="Node">
        <div class="tags-list-item">Node</div>
      </a>
    
      <a 
        href="/tags/Vue%E5%9F%BA%E7%A1%80/" 
        title="Vue基础">
        <div class="tags-list-item">Vue基础</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E5%85%B7/" 
        title="前端工具">
        <div class="tags-list-item">前端工具</div>
      </a>
    
      <a 
        href="/tags/HTML/" 
        title="HTML">
        <div class="tags-list-item">HTML</div>
      </a>
    
      <a 
        href="/tags/JS%E8%BF%9B%E9%98%B6/" 
        title="JS进阶">
        <div class="tags-list-item">JS进阶</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80%E5%BC%82%E6%AD%A5/" 
        title="JS基础异步">
        <div class="tags-list-item">JS基础异步</div>
      </a>
    
      <a 
        href="/tags/JS%E5%9F%BA%E7%A1%80ES6/" 
        title="JS基础ES6">
        <div class="tags-list-item">JS基础ES6</div>
      </a>
    
      <a 
        href="/tags/CSS%E8%BF%9B%E9%98%B6/" 
        title="CSS进阶">
        <div class="tags-list-item">CSS进阶</div>
      </a>
    
      <a 
        href="/tags/%E5%89%8D%E7%AB%AF%E5%B7%A5%E7%A8%8B%E5%8C%96/" 
        title="前端工程化">
        <div class="tags-list-item">前端工程化</div>
      </a>
    
      <a 
        href="/tags/%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96/" 
        title="性能优化">
        <div class="tags-list-item">性能优化</div>
      </a>
    
      <a 
        href="/tags/CSS%E5%92%8CDOM/" 
        title="CSS和DOM">
        <div class="tags-list-item">CSS和DOM</div>
      </a>
    
      <a 
        href="/tags/%E7%A7%BB%E5%8A%A8web/" 
        title="移动web">
        <div class="tags-list-item">移动web</div>
      </a>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
            <main class="main-column">
              
<article class="card card-content">
  <header>
    <h1 class="post-title">
      15-内置对象 String：字符串的常见方法
    </h1>
  </header>
  <div class="post-meta post-show-meta">
    <time datetime="2004-02-29T16:00:00.000Z">
      <i 
        class="iconfont icon-calendar" 
        style="margin-right: 2px;">
      </i>
      <span>2004-03-01</span>
    </time>
    
      <span class="dot"></span>
      
        <a 
          href="/categories/JS%E5%9F%BA%E7%A1%80/" 
          class="post-meta-link">
          JS基础
        </a>
      
    
    
      <span class="dot"></span>
      <span>3.9k 字</span>
    
  </div>
  
    <div 
      class="post-meta post-show-meta" 
      style="margin-top: -10px;">
      <div style="display: flex; align-items: center;">
        <i 
          class="iconfont icon-biaoqian" 
          style="margin-right: 2px; font-size: 1.15rem;">
        </i>
        
          
          <a 
            href="/tags/JS%E5%9F%BA%E7%A1%80/" 
            class="post-meta-link">
            JS基础
          </a>
        
      </div>
    </div>
  
  </header>
  <div 
    id="section" 
    class="post-content">
    <p><ArticleTopAd></ArticleTopAd></p>
<h2 id="内置对象简介"><a href="#内置对象简介" class="headerlink" title="内置对象简介"></a>内置对象简介</h2><blockquote>
<p>JavaScript 中的对象分为3种：自定义对象、内置对象、浏览器对象。</p>
</blockquote>
<blockquote>
<p>前面两种对象：是JS的基础内容，属于 ECMAScript； 第三个浏览器对象：属于JS独有，即 JS 内置的API。</p>
</blockquote>
<p><strong>内置对象</strong>：就是指这个语言自带的一些对象，供开发者使用，这些对象提供了一些常用或者基本而必要的功能（属性和方法）。</p>
<p>内置对象最大的优点就是帮助我们快速开发。</p>
<p><strong>JavaScript的内置对象</strong>：</p>
<table>
<thead>
<tr>
<th align="left">内置对象</th>
<th align="left">对象说明</th>
</tr>
</thead>
<tbody><tr>
<td align="left">Arguments</td>
<td align="left">函数参数集合</td>
</tr>
<tr>
<td align="left">Array</td>
<td align="left">数组</td>
</tr>
<tr>
<td align="left">Boolean</td>
<td align="left">布尔对象</td>
</tr>
<tr>
<td align="left">Math</td>
<td align="left">数学对象</td>
</tr>
<tr>
<td align="left">Date</td>
<td align="left">日期时间</td>
</tr>
<tr>
<td align="left">Error</td>
<td align="left">异常对象</td>
</tr>
<tr>
<td align="left">Function</td>
<td align="left">函数构造器</td>
</tr>
<tr>
<td align="left">Number</td>
<td align="left">数值对象</td>
</tr>
<tr>
<td align="left">Object</td>
<td align="left">基础对象</td>
</tr>
<tr>
<td align="left">RegExp</td>
<td align="left">正则表达式对象</td>
</tr>
<tr>
<td align="left">String</td>
<td align="left">字符串对象</td>
</tr>
</tbody></table>
<h2 id="字符串前言"><a href="#字符串前言" class="headerlink" title="字符串前言"></a>字符串前言</h2><blockquote>
<p>在日常开发中，String 对象（字符串对象）的使用频率是非常高的。所以有必要详细介绍。</p>
</blockquote>
<p>需要注意的是：<strong>字符串的所有方法，都不会改变原字符串</strong>（字符串的不可变性），操作完成后会返回一个新的值。</p>
<p>字符串的常见方法如下。</p>
<h2 id="查找字符串"><a href="#查找字符串" class="headerlink" title="查找字符串"></a>查找字符串</h2><h3 id="1、indexOf-x2F-lastIndexOf-：获取字符串中指定内容的索引"><a href="#1、indexOf-x2F-lastIndexOf-：获取字符串中指定内容的索引" class="headerlink" title="1、indexOf()&#x2F;lastIndexOf()：获取字符串中指定内容的索引"></a>1、indexOf()&#x2F;lastIndexOf()：获取字符串中指定内容的索引</h3><blockquote>
<p>这个方法，是使用频率最高的一个方法。</p>
</blockquote>
<p><strong>语法 1</strong>：</p>
<pre class="highlight"><span class="line">索引值 = str.<span class="title function_">indexOf</span>(想要查询的字符串);</span><br></pre>

<p>备注：<code>indexOf()</code> 是从前向后查找字符串的位置。同理，<code>lastIndexOf()</code>是从后向前寻找。</p>
<p><strong>解释</strong>：可以检索一个字符串中是否含有指定内容。如果字符串中含有该内容，则会返回其<strong>第一次出现</strong>的索引；如果没有找到指定的内容，则返回 -1。</p>
<p>因此可以得出一个重要技巧：</p>
<ul>
<li><p><strong>如果获取的索引值为 0，说明字符串是以查询的参数为开头的</strong>。</p>
</li>
<li><p>如果获取的索引值为-1，说明这个字符串中没有指定的内容。</p>
</li>
</ul>
<p>举例 1：(查找单个字符)</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> str = <span class="string">&#x27;abcdea&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//给字符查索引(索引值为0,说明字符串以查询的参数为开头)</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">indexOf</span>(<span class="string">&#x27;c&#x27;</span>));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;c&#x27;</span>));</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">indexOf</span>(<span class="string">&#x27;a&#x27;</span>));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">lastIndexOf</span>(<span class="string">&#x27;a&#x27;</span>));</span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_1420.png" class="lozad post-image"src="http://img.smyhvae.com/20180202_1420.png"></p>
<p>举例 2：（查找字符串）</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">indexOf</span>(<span class="string">&#x27;yi&#x27;</span>)); <span class="comment">// 打印结果：6</span></span><br></pre>

<p><strong>语法 2</strong>：</p>
<p>这个方法还可以指定第二个参数，用来指定查找的<strong>起始位置</strong>。语法如下：</p>
<pre class="highlight"><span class="line">索引值 = str.<span class="title function_">indexOf</span>(想要查询的字符串, [起始位置]);</span><br></pre>

<p>举例 3：（两个参数时，需要特别注意）</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line">result = str.<span class="title function_">indexOf</span>(<span class="string">&#x27;a&#x27;</span>, <span class="number">3</span>); <span class="comment">// 从下标为3的位置开始查找 &#x27;a&#x27;这个字符 【重要】</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">// 打印结果：9</span></span><br></pre>

<p>上方代码中，<code>indexOf()</code>方法中携带了两个参数，具体解释请看注释。</p>
<h3 id="indexOf-举例"><a href="#indexOf-举例" class="headerlink" title="indexOf 举例"></a>indexOf 举例</h3><p><strong>案例</strong>：查找字符串”qianguyihao”中，所有 <code>a</code> 出现的位置以及次数。</p>
<p>思路：</p>
<p>（1）先查找第一个 a 出现的位置。</p>
<p>（2）只要 indexOf 返回的结果不是 -1 就继续往后查找。</p>
<p>（3）因为 indexOf 只能查找到第一个，所以后面的查找，可以利用第二个参数，在当前索引加 1，从而继续查找。</p>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> index = str.<span class="title function_">indexOf</span>(<span class="string">&#x27;a&#x27;</span>);</span><br><span class="line"><span class="keyword">var</span> num = <span class="number">0</span>;</span><br><span class="line"><span class="keyword">while</span> (index !== -<span class="number">1</span>) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(index);</span><br><span class="line">    num++; <span class="comment">// 每打印一次，就计数一次</span></span><br><span class="line">    index = str.<span class="title function_">indexOf</span>(<span class="string">&#x27;o&#x27;</span>, index + <span class="number">1</span>);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;a 出现的次数是: &#x27;</span> + num);</span><br></pre>


<h3 id="2、search-：获取字符串中指定内容的索引（参数里一般是正则）"><a href="#2、search-：获取字符串中指定内容的索引（参数里一般是正则）" class="headerlink" title="2、search()：获取字符串中指定内容的索引（参数里一般是正则）"></a>2、search()：获取字符串中指定内容的索引（参数里一般是正则）</h3><p><strong>语法</strong>：</p>
<pre class="highlight"><span class="line">索引值 = str.<span class="title function_">search</span>(想要查找的字符串);</span><br><span class="line">索引值 = str.<span class="title function_">search</span>(正则表达式);</span><br><span class="line"></span><br></pre>

<p>备注：<code>search()</code> 方法里的参数，既可以传字符串，也可以传正则表达式。</p>
<p><strong>解释</strong>：可以检索一个字符串中是否含有指定内容。如果字符串中含有该内容，则会返回其<strong>第一次出现</strong>的索引；如果没有找到指定的内容，则返回 -1。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">search</span>(<span class="string">&#x27;yi&#x27;</span>)); <span class="comment">// 打印结果：6</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">search</span>(<span class="regexp">/yi/i</span>)); <span class="comment">// 打印结果：6</span></span><br></pre>

<p>备注：上方的<code>/yi/i</code>采用的是正则表达式的写法，意思是，让 name去匹配字符<code>yi</code>，忽略大小写。我们在后面会专门介绍正则表达式。</p>
<h3 id="3、includes-：字符串中是否包含指定的内容"><a href="#3、includes-：字符串中是否包含指定的内容" class="headerlink" title="3、includes()：字符串中是否包含指定的内容"></a>3、includes()：字符串中是否包含指定的内容</h3><p><strong>语法</strong>：</p>
<pre class="highlight"><span class="line">布尔值 = str.<span class="title function_">includes</span>(想要查找的字符串, [position]);</span><br></pre>

<p><strong>解释</strong>：判断一个字符串中是否含有指定内容。如果字符串中含有该内容，则会返回 true；否则返回 false。</p>
<p>参数中的 <code>position</code>：如果不指定，则默认为0；如果指定，则规定了检索的起始位置。</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">includes</span>(<span class="string">&#x27;yi&#x27;</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">includes</span>(<span class="string">&#x27;haha&#x27;</span>)); <span class="comment">// 打印结果：false</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">includes</span>(<span class="string">&#x27;yi&#x27;</span>,<span class="number">7</span>)); <span class="comment">// 打印结果：false</span></span><br></pre>


<h3 id="4、startsWith-：字符串是否以指定的内容开头"><a href="#4、startsWith-：字符串是否以指定的内容开头" class="headerlink" title="4、startsWith()：字符串是否以指定的内容开头"></a>4、startsWith()：字符串是否以指定的内容开头</h3><p><strong>语法</strong>：</p>
<pre class="highlight"><span class="line">布尔值 = str.<span class="title function_">startsWith</span>(想要查找的内容, [position]);</span><br></pre>

<p><strong>解释</strong>：判断一个字符串是否以指定的子字符串开头。如果是，则返回 true；否则返回 false。</p>
<p><strong>参数中的position</strong>：</p>
<ul>
<li><p>如果不指定，则默认为0。</p>
</li>
<li><p>如果指定，则规定了<strong>检索的起始位置</strong>。检索的范围包括：这个指定位置开始，直到字符串的末尾。即：[position, str.length)</p>
</li>
</ul>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;abcdefg&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;a&#x27;</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;b&#x27;</span>)); <span class="comment">// 打印结果：false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 因为指定了起始位置为3，所以是在 defg 这个字符串中检索。</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;d&#x27;</span>,<span class="number">3</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">startsWith</span>(<span class="string">&#x27;c&#x27;</span>,<span class="number">3</span>)); <span class="comment">// 打印结果：false</span></span><br></pre>

<h3 id="5、endsWith-：字符串是否以指定的内容结尾"><a href="#5、endsWith-：字符串是否以指定的内容结尾" class="headerlink" title="5、endsWith()：字符串是否以指定的内容结尾"></a>5、endsWith()：字符串是否以指定的内容结尾</h3><p><strong>语法</strong>：</p>
<pre class="highlight"><span class="line">布尔值 = str.<span class="title function_">endsWith</span>(想要查找的内容, [position]);</span><br></pre>

<p><strong>解释</strong>：判断一个字符串是否以指定的子字符串结尾。如果是，则返回 true；否则返回 false。</p>
<p><strong>参数中的position</strong>：</p>
<ul>
<li><p>如果不指定，则默认为 str.length。</p>
</li>
<li><p>如果指定，则规定了<strong>检索的结束位置</strong>。检索的范围包括：从第一个字符串开始，直到这个指定的位置。即：[0, position)</p>
</li>
<li><p>或者你可以这样简单理解：endsWith() 方法里的position，表示<strong>检索的长度</strong>。</p>
</li>
</ul>
<p>注意：startsWith() 和 endsWith()这两个方法，他们的 position 的含义是不同的，请仔细区分。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;abcdefg&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;g&#x27;</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;f&#x27;</span>)); <span class="comment">// 打印结果：false</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 因为指定了截止位置为3，所以是在 abc 这个长度为3字符串中检索</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;c&#x27;</span>, <span class="number">3</span>)); <span class="comment">// 打印结果：true</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">endsWith</span>(<span class="string">&#x27;d&#x27;</span>, <span class="number">3</span>)); <span class="comment">// 打印结果：false</span></span><br></pre>

<p>注意看上方的注释。</p>
<p>参考链接：<a target="_blank" rel="noopener" href="https://www.softwhy.com/article-2885-1.html">JavaScript endsWith()介绍</a></p>
<h2 id="获取指定位置的字符"><a href="#获取指定位置的字符" class="headerlink" title="获取指定位置的字符"></a>获取指定位置的字符</h2><h3 id="1、charAt-index"><a href="#1、charAt-index" class="headerlink" title="1、charAt(index)"></a>1、charAt(index)</h3><p>语法：</p>
<pre class="highlight"><span class="line">字符 = str.<span class="title function_">charAt</span>(index);</span><br></pre>

<p>解释：返回字符串指定位置的字符。这里的 <code>str.charAt(index)</code>和<code>str[index]</code>的效果是一样的。</p>
<p>注意：字符串中第一个字符的下标是 0。如果参数 index 不在 [0, string.length) 之间，该方法将返回一个空字符串。</p>
<p><strong>代码举例</strong>：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str = <span class="keyword">new</span> <span class="title class_">String</span>(<span class="string">&#x27;smyhvae&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; str.<span class="property">length</span>; i++) &#123;</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">charAt</span>(i));</span><br><span class="line">&#125;</span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_1401.png" class="lozad post-image"src="http://img.smyhvae.com/20180202_1401.png"></p>
<p>上面这个例子一般不用。一般打印数组和 json 的时候用索引，打印 String 不建议用索引。</p>
<h3 id="2、str-index"><a href="#2、str-index" class="headerlink" title="2、str[index]"></a>2、str[index]</h3><p><code>str.charAt(index)</code>和<code>str[index]</code>的效果是一样的，不再赘述。区别在于：<code>str[index]</code>是 H5 标准里新增的特性。</p>
<h3 id="3、charCodeAt-index"><a href="#3、charCodeAt-index" class="headerlink" title="3、charCodeAt(index)"></a>3、charCodeAt(index)</h3><p>语法：</p>
<pre class="highlight"><span class="line">字符 = str.<span class="title function_">charCodeAt</span>(index);</span><br></pre>

<p>解释：返回字符串指定位置的字符的 Unicode 编码。不会修改原字符串。</p>
<p>在实际应用中，通过这个方法，我们可以判断用户按下了哪个按键。</p>
<p><strong>代码举例</strong>：打印字符串的<strong>占位长度</strong>。</p>
<p>提示：一个英文占一个位置，一个中文占两个位置。</p>
<p>思路：判断该字符是否在 0-127 之间（在的话是英文，不在是非英文）。</p>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//    sort();   底层用到了charCodeAt();</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> str = <span class="string">&#x27;I love my country!我你爱中国！&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//需求：求一个字符串占有几个字符位。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//思路；如果是英文，站一个字符位，如果不是英文占两个字符位。</span></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//技术点：判断该字符是否在0-127之间。（在的话是英文，不在是非英文）</span></span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">alert</span>(<span class="title function_">getZFWlength</span>(str));</span></span><br><span class="line"><span class="language-javascript">    <span class="title function_">alert</span>(str.<span class="property">length</span>);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//定义方法：字符位</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">function</span> <span class="title function_">getZFWlength</span>(<span class="params">string</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//定义一个计数器</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> count = <span class="number">0</span>;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; string.<span class="property">length</span>; i++) &#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//对每一位字符串进行判断，如果Unicode编码在0-127，计数器+1；否则+2</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">if</span> (string.<span class="title function_">charCodeAt</span>(i) &lt; <span class="number">128</span> &amp;&amp; string.<span class="title function_">charCodeAt</span>(i) &gt;= <span class="number">0</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">                count++;</span></span><br><span class="line"><span class="language-javascript">            &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">                count += <span class="number">2</span>;</span></span><br><span class="line"><span class="language-javascript">            &#125;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">return</span> count;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre>

<p>打印结果：</p>
<pre class="highlight"><span class="line">30</span><br><span class="line">24</span><br></pre>

<p>从打印结果可以看出：字符串的长度是 24，但是却占了 30 个字符位（一个中文占两个字符位）。</p>
<p>另外，sort()方法其实底层也是用到了 charCodeAt()，因为用到了 Unicode 编码。</p>
<h2 id="字符串截取"><a href="#字符串截取" class="headerlink" title="字符串截取"></a>字符串截取</h2><h3 id="1、slice"><a href="#1、slice" class="headerlink" title="1、slice()"></a>1、slice()</h3><blockquote>
<p>slice() 方法用的最多。</p>
</blockquote>
<p>语法：</p>
<pre class="highlight"><span class="line">新字符串 = str.<span class="title function_">slice</span>(开始索引, 结束索引); <span class="comment">//两个参数都是索引值。包左不包右。</span></span><br></pre>

<p>解释：从字符串中截取指定的内容。不会修改原字符串，而是将截取到的内容返回。</p>
<p>注意：上面的参数，包左不包右。参数举例如下：</p>
<ul>
<li><p><code>(2, 5)</code> 截取时，包左不包右。</p>
</li>
<li><p><code>(2)</code> 表示<strong>从指定的索引位置开始，截取到最后</strong>。</p>
</li>
<li><p><code>(-3)</code> 表示从倒数第三个开始，截取到最后。</p>
</li>
<li><p><code>(1, -1)</code> 表示从第一个截取到倒数第一个。</p>
</li>
<li><p><code>(5, 2)</code> 表示前面的大，后面的小，返回值为空。</p>
</li>
</ul>
<h3 id="2、substring"><a href="#2、substring" class="headerlink" title="2、substring()"></a>2、substring()</h3><p>语法：</p>
<pre class="highlight"><span class="line">新字符串 = str.<span class="title function_">substring</span>(开始索引, 结束索引); <span class="comment">//两个参数都是索引值。包左不包右。</span></span><br></pre>

<p>解释：从字符串中截取指定的内容。和<code>slice()</code>类似。</p>
<p><code>substring()</code>和<code>slice()</code>是类似的。但不同之处在于：</p>
<ul>
<li><p><code>substring()</code>不能接受负值作为参数。如果传递了一个<strong>负值</strong>，则默认使用 0。</p>
</li>
<li><p><code>substring()</code>还会自动调整参数的位置，如果第二个参数小于第一个，则自动交换。比如说， <code>substring(1, 0)</code>相当于截取的是第一个字符。</p>
</li>
</ul>
<h3 id="3、substr"><a href="#3、substr" class="headerlink" title="3、substr()"></a>3、substr()</h3><p>语法：</p>
<pre class="highlight"><span class="line">字符串 = str.<span class="title function_">substr</span>(开始索引, 截取的长度);</span><br></pre>

<p>解释：从字符串中截取指定的内容。不会修改原字符串，而是将截取到的内容返回。</p>
<p>注意，这个方法的第二个参数<strong>截取的长度</strong>，不是结束索引。</p>
<p>参数举例：</p>
<ul>
<li><p><code>(2,4)</code> 从索引值为 2 的字符开始，截取 4 个字符。</p>
</li>
<li><p><code>(1)</code> 从指定位置开始，截取到最后。</p>
</li>
<li><p><code>(-3)</code> 从倒数第几个开始，截取到最后。</p>
</li>
</ul>
<p>备注：ECMAscript 没有对 <code>substr()</code> 方法进行标准化，因此不建议使用它。</p>
<h2 id="String-fromCharCode"><a href="#String-fromCharCode" class="headerlink" title="String.fromCharCode()"></a>String.fromCharCode()</h2><p><code>String.fromCharCode()</code>：根据字符的 Unicode 编码获取字符。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> result1 = <span class="title class_">String</span>.<span class="title function_">fromCharCode</span>(<span class="number">72</span>);</span><br><span class="line"><span class="keyword">var</span> result2 = <span class="title class_">String</span>.<span class="title function_">fromCharCode</span>(<span class="number">20013</span>);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result1); <span class="comment">// 打印结果：H</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result2); <span class="comment">// 打印结果：中</span></span><br></pre>

<h2 id="concat"><a href="#concat" class="headerlink" title="concat()"></a>concat()</h2><p>语法：</p>
<pre class="highlight"><span class="line">新字符串 = str1.<span class="title function_">concat</span>(str2)； <span class="comment">//连接两个字符串</span></span><br></pre>

<p>解释：字符串的连接。</p>
<p>这种方法基本不用，直接把两个字符串相加就好。</p>
<p>是的，你会发现，数组中也有<code>concat()</code>方法，用于数组的连接。这个方法在数组中用得挺多的。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str1 = <span class="string">&#x27;qiangu&#x27;</span>;</span><br><span class="line"><span class="keyword">var</span> str2 = <span class="string">&#x27;yihao&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> result = str1.<span class="title function_">concat</span>(str2);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(result); <span class="comment">// 打印结果：qianguyihao</span></span><br></pre>

<h2 id="split-：字符串转换为数组-【重要】"><a href="#split-：字符串转换为数组-【重要】" class="headerlink" title="split()：字符串转换为数组 【重要】"></a>split()：字符串转换为数组 【重要】</h2><p>语法：</p>
<pre class="highlight"><span class="line">新的数组 = str.<span class="title function_">split</span>(分隔符);</span><br></pre>

<p>解释：通过指定的分隔符，将一个字符串拆分成一个<strong>数组</strong>。不会改变原字符串。</p>
<p>备注：<code>split()</code>这个方法在实际开发中用得非常多。一般来说，从接口拿到的 json 数据中，经常会收到类似于<code>&quot;q, i, a, n&quot;</code>这样的字符串，前端需要将这个字符串拆分成<code>[&#39;q&#39;, &#39;i&#39;, &#39;a&#39;, &#39;n&#39;]</code>数组，这个时候<code>split()</code>方法就派上用场了。</p>
<p><strong>代码举例 1</strong>：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;qian, gu, yi, hao&#x27;</span>; <span class="comment">// 用逗号隔开的字符串</span></span><br><span class="line"><span class="keyword">var</span> array = str.<span class="title function_">split</span>(<span class="string">&#x27;,&#x27;</span>); <span class="comment">// 将字符串 str 拆分成数组，通过逗号来拆分</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(array); <span class="comment">// 打印结果是数组：[&quot;qian&quot;, &quot; gu&quot;, &quot; yi&quot;, &quot; hao&quot;]</span></span><br></pre>

<p><strong>代码举例 2</strong>：</p>
<pre class="highlight"><span class="line"><span class="comment">//split()方法：字符串变数组</span></span><br><span class="line"><span class="keyword">var</span> str3 = <span class="string">&#x27;千古壹号|qianguyihao|许嵩&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;结果1：&#x27;</span> +str3.<span class="title function_">split</span>()); <span class="comment">// 无参数，表示：把整个字符串作为一个元素添加到数组中。</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str3.<span class="title function_">split</span>(<span class="string">&#x27;&#x27;</span>)); <span class="comment">// 参数为空字符串，则表示：分隔字符串中每一个字符，分别添加到数组中</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str3.<span class="title function_">split</span>(<span class="string">&#x27;|&#x27;</span>)); <span class="comment">// 参数为指定字符，表示：用 &#x27;|&#x27; 分隔字符串。此分隔符将不会出现在数组的任意一个元素中</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str3.<span class="title function_">split</span>(<span class="string">&#x27;许&#x27;</span>)); <span class="comment">// 同上</span></span><br></pre>

<p>打印结果：（都是数组）</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20200611_2050.png" class="lozad post-image"src="http://img.smyhvae.com/20200611_2050.png"></p>
<h2 id="replace"><a href="#replace" class="headerlink" title="replace()"></a>replace()</h2><p>语法：</p>
<pre class="highlight"><span class="line">新的字符串 = str.<span class="title function_">replace</span>(被替换的子串，新的子串);</span><br></pre>

<p>解释：将字符串中的指定内容，替换为新的内容并返回。不会修改原字符串。</p>
<p>注意：这个方法，默认只会替换第一个被匹配到的字符。如果要全局替换，需要使用正则。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="comment">//replace()方法：替换</span></span><br><span class="line"><span class="keyword">var</span> str2 = <span class="string">&#x27;Today is fine day,today is fine day !&#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str2);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str2.<span class="title function_">replace</span>(<span class="string">&#x27;today&#x27;</span>, <span class="string">&#x27;tomorrow&#x27;</span>)); <span class="comment">//只能替换第一个today</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str2.<span class="title function_">replace</span>(<span class="regexp">/today/gi</span>, <span class="string">&#x27;tomorrow&#x27;</span>)); <span class="comment">//这里用到了正则，才能替换所有的today</span></span><br></pre>

<h2 id="repeat-：重复字符串"><a href="#repeat-：重复字符串" class="headerlink" title="repeat()：重复字符串"></a>repeat()：重复字符串</h2><p>语法：</p>
<pre class="highlight"><span class="line">newStr = str.<span class="title function_">repeat</span>(重复的次数);</span><br></pre>

<p>解释：将字符串重复指定的次数。会返回新的值，不会修改原字符串。</p>
<p>举例1：</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> name = <span class="string">&#x27;qianguyihao&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(name.<span class="title function_">repeat</span>(<span class="number">2</span>)); <span class="comment">// 打印内容：qianguyihaoqianguyihao</span></span><br></pre>

<p>举例2：（模糊字符串的后四位）</p>
<pre class="highlight"><span class="line"><span class="keyword">const</span> telephone = <span class="string">&#x27;13088889999&#x27;</span>;</span><br><span class="line"><span class="keyword">const</span> mix_telephone = telephone.<span class="title function_">slice</span>(<span class="number">0</span>, -<span class="number">4</span>) + <span class="string">&#x27;*&#x27;</span>.<span class="title function_">repeat</span>(<span class="number">4</span>); <span class="comment">// 模糊电话号码的后四位</span></span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(telephone); <span class="comment">// 打印结果：13088889999</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(mix_telephone); <span class="comment">// 打印结果：1308888****</span></span><br></pre>






<h2 id="trim"><a href="#trim" class="headerlink" title="trim()"></a>trim()</h2><p><code>trim()</code>：去除字符串前后的空白。</p>
<p>代码举例：</p>
<pre class="highlight"><span class="line"><span class="comment">//去除字符串前后的空格，trim();</span></span><br><span class="line"><span class="keyword">let</span> str = <span class="string">&#x27;   a   b   c   &#x27;</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str);</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="property">length</span>);</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">trim</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">trim</span>().<span class="property">length</span>);</span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20200607_2132.png" class="lozad post-image"src="http://img.smyhvae.com/20200607_2132.png"></p>
<h2 id="大小写转换"><a href="#大小写转换" class="headerlink" title="大小写转换"></a>大小写转换</h2><p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;abcdEFG&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">//转换成小写</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">toLowerCase</span>());</span><br><span class="line"></span><br><span class="line"><span class="comment">//转换成大写</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">toUpperCase</span>());</span><br></pre>

<h2 id="html-方法"><a href="#html-方法" class="headerlink" title="html 方法"></a>html 方法</h2><ul>
<li><p>anchor() 创建 a 链接</p>
</li>
<li><p>big()</p>
</li>
<li><p>sub()</p>
</li>
<li><p>sup()</p>
</li>
<li><p>link()</p>
</li>
<li><p>bold()</p>
</li>
</ul>
<p>注意，str.link() 返回值是字符串。</p>
<p>举例：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str = <span class="string">&#x27;你好&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">anchor</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">big</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">sub</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">sup</span>());</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">link</span>(<span class="string">&#x27;http://www.baidu.com&#x27;</span>));</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(str.<span class="title function_">bold</span>());</span><br></pre>

<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_1536.png" class="lozad post-image"src="http://img.smyhvae.com/20180202_1536.png"></p>
<h2 id="字符串练习"><a href="#字符串练习" class="headerlink" title="字符串练习"></a>字符串练习</h2><p><strong>练习 1</strong>：”smyhvaevaesmyh”查找字符串中所有 m 出现的位置。</p>
<p>代码实现：</p>
<pre class="highlight"><span class="line"><span class="keyword">var</span> str2 = <span class="string">&#x27;smyhvaevaesmyh&#x27;</span>;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; str2.<span class="property">length</span>; i++) &#123;</span><br><span class="line">    <span class="comment">//如果指定位置的符号=== &quot;o&quot;</span></span><br><span class="line">    <span class="comment">//str2[i]</span></span><br><span class="line">    <span class="keyword">if</span> (str2.<span class="title function_">charAt</span>(i) === <span class="string">&#x27;m&#x27;</span>) &#123;</span><br><span class="line">        <span class="variable language_">console</span>.<span class="title function_">log</span>(i);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre>

<p><strong>练习 2</strong>：判断一个字符串中出现次数最多的字符，统计这个次数</p>
<pre class="highlight"><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> str2 = <span class="string">&#x27;smyhvaevaesmyhvae&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//定义一个json，然后判断json中是够有该属性，如果有该属性，那么值+1;否则创建一个该属性，并赋值为1；</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> json = &#123;&#125;;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">for</span> (<span class="keyword">var</span> i = <span class="number">0</span>; i &lt; str2.<span class="property">length</span>; i++) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//判断：如果有该属性，那么值+1;否则创建一个该属性，并赋值为1；</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> key = str2.<span class="title function_">charAt</span>(i);</span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (json[key] === <span class="literal">undefined</span>) &#123;</span></span><br><span class="line"><span class="language-javascript">            json[key] = <span class="number">1</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125; <span class="keyword">else</span> &#123;</span></span><br><span class="line"><span class="language-javascript">            json[key] += <span class="number">1</span>;</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(json);</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">&#x27;----------------&#x27;</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="comment">//获取json中属性值最大的选项</span></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> maxKey = <span class="string">&#x27;&#x27;</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> maxValue = <span class="number">0</span>;</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">for</span> (<span class="keyword">var</span> k <span class="keyword">in</span> json) &#123;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//        if(maxKey == &quot;&quot;)&#123;</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//            maxKey = k;</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//            maxValue = json[k];</span></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//        &#125;else&#123;</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">if</span> (json[k] &gt; maxValue) &#123;</span></span><br><span class="line"><span class="language-javascript">            maxKey = k;</span></span><br><span class="line"><span class="language-javascript">            maxValue = json[k];</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">//        &#125;</span></span></span><br><span class="line"><span class="language-javascript">    &#125;</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(maxKey);</span></span><br><span class="line"><span class="language-javascript">    <span class="variable language_">console</span>.<span class="title function_">log</span>(maxValue);</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre>

<p>打印结果：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="http://img.smyhvae.com/20180202_1540.png" class="lozad post-image"src="http://img.smyhvae.com/20180202_1540.png"></p>
<h2 id="赞赏作者"><a href="#赞赏作者" class="headerlink" title="赞赏作者"></a>赞赏作者</h2><p>创作不易，你的赞赏和认可，是我更新的最大动力：</p>
<p><img  srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20viewBox='0%200%20300%20300'%3E%3C/svg%3E" data-src="https://img.smyhvae.com/20220401_1800.jpg" class="lozad post-image"src="https://img.smyhvae.com/20220401_1800.jpg"></p>

  </div>
  <div>
    
      <div 
        class="post-note note-warning copyright" 
        style="margin-top: 42px">
        <p>
          <span style="font-weight: bold;">作者：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="/about">
            zwl5670
          </a>
        </p>
        <p>
          <span style="font-weight: bold;">文章链接：</span><a 
            target="_blank" 
            rel="nofollow noopener noreferrer" 
            href="https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/15-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%20String%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%B8%B8%E8%A7%81%E6%96%B9%E6%B3%95/">
            https://zwl5670.gitee.io/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/15-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%20String%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%9A%84%E5%B8%B8%E8%A7%81%E6%96%B9%E6%B3%95/
          </a>
        </p>
        <p><span style="font-weight: bold;">版权声明：</span>本博客所有文章除特别声明外，均采用<a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">CC BY-NC-SA 4.0 协议</a>。转载请注明出处！</p>
      </div>
    
  </div>
</article>
<div class="nav">
  
    <div class="nav-item-prev">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/16-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ANumber%E5%92%8CMath/" 
        class="nav-link">
        <i class="iconfont icon-left nav-prev-icon"></i>
        <div>
          <div class="nav-label">上一篇</div>
          
            <div class="nav-title">16-内置对象：Number和Math </div>
          
        </div>
      </a>
    </div>
  
  
    <div class="nav-item-next">
      <a 
        href="/2004/03/01/04-JavaScript%E5%9F%BA%E7%A1%80/17-%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%EF%BC%9ADate/" 
        class="nav-link">
        <div>
          <div class="nav-label">下一篇</div>
          
            <div class="nav-title">17-内置对象：Date </div>
          
        </div>
        <i class="iconfont icon-right nav-next-icon"></i>
      </a>
    </div>
  
</div>

<div 
  class="card card-content toc-card" 
  id="mobiletoc">
  <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%E7%AE%80%E4%BB%8B"><span class="toc-text">内置对象简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%89%8D%E8%A8%80"><span class="toc-text">字符串前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9F%A5%E6%89%BE%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">查找字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81indexOf-x2F-lastIndexOf-%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%8C%87%E5%AE%9A%E5%86%85%E5%AE%B9%E7%9A%84%E7%B4%A2%E5%BC%95"><span class="toc-text">1、indexOf()&#x2F;lastIndexOf()：获取字符串中指定内容的索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#indexOf-%E4%B8%BE%E4%BE%8B"><span class="toc-text">indexOf 举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81search-%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%8C%87%E5%AE%9A%E5%86%85%E5%AE%B9%E7%9A%84%E7%B4%A2%E5%BC%95%EF%BC%88%E5%8F%82%E6%95%B0%E9%87%8C%E4%B8%80%E8%88%AC%E6%98%AF%E6%AD%A3%E5%88%99%EF%BC%89"><span class="toc-text">2、search()：获取字符串中指定内容的索引（参数里一般是正则）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81includes-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9"><span class="toc-text">3、includes()：字符串中是否包含指定的内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81startsWith-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E4%BB%A5%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9%E5%BC%80%E5%A4%B4"><span class="toc-text">4、startsWith()：字符串是否以指定的内容开头</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5%E3%80%81endsWith-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E4%BB%A5%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9%E7%BB%93%E5%B0%BE"><span class="toc-text">5、endsWith()：字符串是否以指定的内容结尾</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E4%BD%8D%E7%BD%AE%E7%9A%84%E5%AD%97%E7%AC%A6"><span class="toc-text">获取指定位置的字符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81charAt-index"><span class="toc-text">1、charAt(index)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81str-index"><span class="toc-text">2、str[index]</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81charCodeAt-index"><span class="toc-text">3、charCodeAt(index)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%88%AA%E5%8F%96"><span class="toc-text">字符串截取</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81slice"><span class="toc-text">1、slice()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81substring"><span class="toc-text">2、substring()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81substr"><span class="toc-text">3、substr()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#String-fromCharCode"><span class="toc-text">String.fromCharCode()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#concat"><span class="toc-text">concat()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#split-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B0%E7%BB%84-%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">split()：字符串转换为数组 【重要】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#replace"><span class="toc-text">replace()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#repeat-%EF%BC%9A%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">repeat()：重复字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#trim"><span class="toc-text">trim()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%A7%E5%B0%8F%E5%86%99%E8%BD%AC%E6%8D%A2"><span class="toc-text">大小写转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#html-%E6%96%B9%E6%B3%95"><span class="toc-text">html 方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BB%83%E4%B9%A0"><span class="toc-text">字符串练习</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
</div>
            </main>
            <aside class="right-column">
              <div class="sticky-widescreen">
  
  
    <article class="card card-content toc-card">
      <div class="toc-header">
  <i 
    class="iconfont icon-menu" 
    style="padding-right: 2px;">
  </i>目录
</div>
<ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%86%85%E7%BD%AE%E5%AF%B9%E8%B1%A1%E7%AE%80%E4%BB%8B"><span class="toc-text">内置对象简介</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%89%8D%E8%A8%80"><span class="toc-text">字符串前言</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%9F%A5%E6%89%BE%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">查找字符串</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81indexOf-x2F-lastIndexOf-%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%8C%87%E5%AE%9A%E5%86%85%E5%AE%B9%E7%9A%84%E7%B4%A2%E5%BC%95"><span class="toc-text">1、indexOf()&#x2F;lastIndexOf()：获取字符串中指定内容的索引</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#indexOf-%E4%B8%BE%E4%BE%8B"><span class="toc-text">indexOf 举例</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81search-%EF%BC%9A%E8%8E%B7%E5%8F%96%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%8C%87%E5%AE%9A%E5%86%85%E5%AE%B9%E7%9A%84%E7%B4%A2%E5%BC%95%EF%BC%88%E5%8F%82%E6%95%B0%E9%87%8C%E4%B8%80%E8%88%AC%E6%98%AF%E6%AD%A3%E5%88%99%EF%BC%89"><span class="toc-text">2、search()：获取字符串中指定内容的索引（参数里一般是正则）</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81includes-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%B8%AD%E6%98%AF%E5%90%A6%E5%8C%85%E5%90%AB%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9"><span class="toc-text">3、includes()：字符串中是否包含指定的内容</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4%E3%80%81startsWith-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E4%BB%A5%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9%E5%BC%80%E5%A4%B4"><span class="toc-text">4、startsWith()：字符串是否以指定的内容开头</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5%E3%80%81endsWith-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%98%AF%E5%90%A6%E4%BB%A5%E6%8C%87%E5%AE%9A%E7%9A%84%E5%86%85%E5%AE%B9%E7%BB%93%E5%B0%BE"><span class="toc-text">5、endsWith()：字符串是否以指定的内容结尾</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E4%BD%8D%E7%BD%AE%E7%9A%84%E5%AD%97%E7%AC%A6"><span class="toc-text">获取指定位置的字符</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81charAt-index"><span class="toc-text">1、charAt(index)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81str-index"><span class="toc-text">2、str[index]</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81charCodeAt-index"><span class="toc-text">3、charCodeAt(index)</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E6%88%AA%E5%8F%96"><span class="toc-text">字符串截取</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1%E3%80%81slice"><span class="toc-text">1、slice()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2%E3%80%81substring"><span class="toc-text">2、substring()</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3%E3%80%81substr"><span class="toc-text">3、substr()</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#String-fromCharCode"><span class="toc-text">String.fromCharCode()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#concat"><span class="toc-text">concat()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#split-%EF%BC%9A%E5%AD%97%E7%AC%A6%E4%B8%B2%E8%BD%AC%E6%8D%A2%E4%B8%BA%E6%95%B0%E7%BB%84-%E3%80%90%E9%87%8D%E8%A6%81%E3%80%91"><span class="toc-text">split()：字符串转换为数组 【重要】</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#replace"><span class="toc-text">replace()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#repeat-%EF%BC%9A%E9%87%8D%E5%A4%8D%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">repeat()：重复字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#trim"><span class="toc-text">trim()</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%A4%A7%E5%B0%8F%E5%86%99%E8%BD%AC%E6%8D%A2"><span class="toc-text">大小写转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#html-%E6%96%B9%E6%B3%95"><span class="toc-text">html 方法</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%AD%97%E7%AC%A6%E4%B8%B2%E7%BB%83%E4%B9%A0"><span class="toc-text">字符串练习</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%B5%9E%E8%B5%8F%E4%BD%9C%E8%80%85"><span class="toc-text">赞赏作者</span></a></li></ol>
    </article>
  
  
  <article class="card card-content">
    <div class="recent-posts-card">
  <div class="recent-posts-header">
    <i 
      class="iconfont icon-wenzhang_huaban" 
      style="padding-right: 2px;">
    </i>最近文章
  </div>
  <div class="recent-posts-list">
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/11-React%20Navive%E5%88%9D%E8%AF%86/"><div class="recent-posts-item-content">11-React Navive初识</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/10-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E4%BD%BF%E7%94%A8customRequest%E6%96%B9%E6%B3%95%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8A%E4%BC%A0%E8%A1%8C%E4%B8%BA/"><div class="recent-posts-item-content">10-AntD框架的upload组件上传图片时使用customRequest方法自定义上传行为</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/09-AntD%E6%A1%86%E6%9E%B6%E7%9A%84upload%E7%BB%84%E4%BB%B6%E4%B8%8A%E4%BC%A0%E5%9B%BE%E7%89%87%E6%97%B6%E9%81%87%E5%88%B0%E7%9A%84%E4%B8%80%E4%BA%9B%E5%9D%91/"><div class="recent-posts-item-content">09-AntD框架的upload组件上传图片时遇到的一些坑</div></a>
      </div>
    
      <div class="recent-posts-item">
        <div class="recent-posts-item-title">2023-03-09</div>
        <a href="/2023/03/09/13-React%E5%9F%BA%E7%A1%80/08-Ant%20Design%E7%9A%84%E5%9F%BA%E6%9C%AC%E4%BD%BF%E7%94%A8/"><div class="recent-posts-item-content">08-Ant Design的基本使用</div></a>
      </div>
    
  </div>
</div>
  </article>
  
  
</div>
            </aside>
          </div>
        </div>
      </div>
    </div>
     
    <footer class="footer">
  <div class="footer-container">
    <div>
      <div class="footer-dsc">
        <span>
          Copyright ©
          
            2023
          
          
        </span>
        &nbsp;
        <a 
          href="/" 
          class="footer-link">
          前端面试
        </a>
      </div>
    </div>

    
      <div class="footer-dsc">
        
          Powered by
          <a 
            href="https://hexo.io/" 
            class="footer-link" 
            target="_blank" 
            rel="nofollow noopener noreferrer">
            &nbsp;Hexo
          </a>
        
        
          <span>&nbsp;|&nbsp;</span>
        
        
          Theme -
          <a 
            href="https://github.com/theme-kaze" 
            class="footer-link" 
            target="_blank"
            rel="nofollow noopener noreferrer">
            &nbsp;Kaze
          </a>
        
      </div>
    
    
    
    
</footer>
 
    
  <a 
    role="button" 
    id="scrollbutton" 
    class="basebutton" 
    aria-label="回到顶部">
    <i class="iconfont icon-arrowleft button-icon"></i>
  </a>

<a 
  role="button" 
  id="menubutton"
  aria-label="menu button"
  class="basebutton">
  <i class="iconfont icon-menu button-icon"></i>
</a>
<a 
  role="button" 
  id="popbutton" 
  class="basebutton" 
  aria-label="控制中心">
  <i class="iconfont icon-expand button-icon"></i>
</a>
<a 
  role="button" 
  id="darkbutton" 
  class="basebutton darkwidget" 
  aria-label="夜色模式">
  <i class="iconfont icon-weather button-icon"></i>
</a>
<a 
  role="button" 
  id="searchbutton" 
  class="basebutton searchwidget" 
  aria-label="搜索">
  <i class="iconfont icon-search button-icon"></i>
</a> 
     
     
     
      <script>
  var addImgLayout = function () {
    var img = document.querySelectorAll('.post-content img')
    var i
    for (i = 0; i < img.length; i++) {
      var wrapper = document.createElement('a')
      wrapper.setAttribute('href', img[i].getAttribute('data-src'))
      wrapper.setAttribute('aria-label', 'illustration')
      wrapper.style.cssText =
        'width: 100%; display: flex; justify-content: center;'
      if (img[i].alt) wrapper.dataset.caption = img[i].alt
      wrapper.dataset.nolink = true
      img[i].before(wrapper)
      wrapper.append(img[i])
      var divWrap = document.createElement('div')
      divWrap.classList.add('gallery')
      wrapper.before(divWrap)
      divWrap.append(wrapper)
    }
    baguetteBox.run('.gallery')
  }
</script>
<script>
  loadScript(
    "/js/lib/lightbox/baguetteBox.min.js",
    addImgLayout
  )
</script>
 
     
     
    <script src="/js/main.js"></script> 
     
    
      <script>
        var addLazyload = function () {
          var observer = lozad('.lozad', {
            load: function (el) {
              el.srcset = el.getAttribute('data-src')
            },
            loaded: function (el) {
              el.classList.add('loaded')
            },
          })
          observer.observe()
        }
      </script>
      <script>
        loadScript('/js/lib/lozad.min.js', addLazyload)
      </script>
    
    <script src="//instant.page/5.1.0" type="module"
      integrity="sha384-by67kQnR+pyfy8yWP4kPO12fHKRLHZPfEsiSXR8u2IKcTdxD805MGUXBzVPnkLHw"></script>
    
    
  </body>
</html>
